angular.module("doList",[])
.directive('appDoList',function () {
    return{
        restrict    : 'E',
        controller  : 'ListCtrl',
        controllerAs: 'app',
        templateUrl : './app/doList/doList.html'
    }
})

.controller("ListCtrl",['$window','$scope','$filter',function ($window,$scope,$filter) {

    var _vm_ = this;
    var thingItem;
    init();
    initList();
    GetThingNum();
    //初始化参数
    function init() {
        //设置事情初始状态
        _vm_.show     = false;
        //模拟数据传入
        _vm_.things   = [];
        //初始化新建事件状态
        _vm_.doStatu  = false;
        //初始化显示文字
        _vm_.statuStr = "全部";
        //从localstroage读取数据
        _vm_.temp     = localStorage.getItem('things');
        //初始化完成数 未完成数 全部数目
        _vm_.comNum   = 0;
        _vm_.undoNum  = 0;
    };
    //初始化列表
    function initList() {
        _vm_.things   = $scope.app.temp ? angular.fromJson($scope.app.temp) : [];
    };
    //初始化所有方法
    _vm_.AddThing      = AddThing;
    _vm_.RemoveShow    = RemoveShow;
    _vm_.ComAll        = ComAll;
    _vm_.RemoveAll     = RemoveAll;
    _vm_.RemoveOne     = RemoveOne;
    _vm_.ShowCom       = ShowCom;
    _vm_.ShowUndone    = ShowUndone;
    _vm_.ShowAll       = ShowAll;
    _vm_.ChangeStatus  = ChangeStatus;
    _vm_.Save          = Save;
    //改变不同状态事件的数量
    function  GetThingNum() {
        _vm_.comNum        = $filter('filter')($scope.app.things,{statu:true}).length;
        _vm_.undoNum       = _vm_.things.length - _vm_.comNum;
    };
    //改變完成狀态
    function ChangeStatus(obj) {
        obj.statu = !obj.statu;
        Save();
        GetThingNum();
    };
    //增加事件功能
    function AddThing() {

        if($scope.dolistForm.$invalid){
            alert("请先输入事件");
        }else {
            thingItem = {
                showIcon: true,
                statu: _vm_.doStatu,
                name: _vm_.yourThing
            };
            _vm_.things.push(thingItem);
            //初始化输入
            _vm_.yourThing = "";
            //跳转到全部显示
            ShowAll();
            Save();
            GetThingNum();
        }
    };
    //显示状态的改变
    function RemoveShow() {
        _vm_.show = true;

    };
    //删除单个事件功能
    function RemoveOne(num) {
        _vm_.things.splice(num,1);
        Save();
        GetThingNum();
    };
    //完成所有事件功能
    function ComAll() {
        angular.forEach(_vm_.things,function (thing) {
            thing.statu = true;
        });
        Save();
        GetThingNum();
    };
    //清空所有事件功能
    function RemoveAll() {
        _vm_.things = [];
        Save();
        GetThingNum();
    };
    //增加过滤方式
    //显示已完成的
    function ShowCom() {
        $scope.filterMethod = {statu: true};
        _vm_.statuStr       = "已完成";
    };
    //显示未完成的
    function ShowUndone() {
        $scope.filterMethod = {statu: false};
        _vm_.statuStr       = "未完成";
    };
    //显示所有的
    function ShowAll() {
        $scope.filterMethod = {};
        _vm_.statuStr       = "全部的";
    };
    //将信息保存至本地localstorage
    function Save() {
        localStorage.setItem('things', angular.toJson($scope.app.things));
    };
}]);

angular.bootstrap(document.body , ['doList']);